package com.company.eos.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.company.eos.common.util.PageUtils;
import com.company.eos.common.R;
import com.company.eos.controller.form.quit.InsertQuitForm;
import com.company.eos.controller.form.quit.SearchQuitByPageForm;
import com.company.eos.controller.form.user.DeleteUserByIdsForm;
import com.company.eos.domain.AuditQuit;
import com.company.eos.service.AuditQuitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;

/**
 * 离职申请模块
 *
 * @author Song, Wei
 * Date: 2022/4/27 20:31
 */
@Slf4j
@RestController
@RequestMapping("/quit")
@Tag(name = "离职模块Web接口")
public class AuditQuitController {

    @Autowired
    private AuditQuitService auditQuitService;

    @PostMapping("/searchQuitByPage")
    @Operation(summary = "查询请假分页数据")
    @SaCheckLogin
    public R searchTables(@Valid @RequestBody SearchQuitByPageForm form) {
        log.info("查询请假分页数据，参数：{}", form);
        @NotNull(message = "page不能为空") @Min(value = 1, message = "page不能小于1") Integer page = form.getPage();
        @NotNull(message = "length不能为空") @Range(min = 10, max = 50, message = "length必须在10~50之间") Integer length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);
        param.put("myId", StpUtil.getLoginIdAsInt());
        if (!(StpUtil.hasPermission("QUIT:SELECT") || StpUtil.hasPermission("ROOT"))) {
            param.put("userId", StpUtil.getLoginIdAsInt());
        }
        PageUtils pageUtils = auditQuitService.searchQuitByPage(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/insert")
    @Operation(summary = "添加离职申请记录")
    @SaCheckLogin
    public R insert(@Valid @RequestBody InsertQuitForm form) {
        log.info("员工申请离职，参数：{}", form);
        // 验证结束时间是不是早于开始时间
        DateTime start = DateUtil.parse(form.getQuitTime());
        if (start.isBeforeOrEquals(new DateTime())) {
            return R.error("离职申请时间不能早于或等于当前时间");
        }

        int userId = StpUtil.getLoginIdAsInt();
        // 查询是否有正在进行的记录
        if (auditQuitService.isExistQuit(userId) >= 1) {
            return R.error("当前已经正在进行中的离职申请，不能再次提交，请等待审批结束");
        }
        DateTime parse = DateUtil.parse(form.getQuitTime());
        AuditQuit quit = new AuditQuit(userId, form.getApplyReason(), new Date(), parse, null, 1);
        int rows = auditQuitService.insert(quit);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/deleteQuitById")
    @Operation(summary = "删除离职申请记录")
    @SaCheckLogin
    public R deleteQuitById(@RequestBody DeleteUserByIdsForm form) {
        log.info("删除离职申请记录，参数：{}", form);
        @NotEmpty(message = "ids不能为空") Integer[] ids = form.getIds();
        int rows = auditQuitService.delete(ids);
        return R.ok().put("rows", rows);
    }

}
